<?php
/**
 *知名校友
 * @author chl
 *
 */
namespace  Teachers\Controller;
use Think\Controller;
class AlumniController extends CommonController
{
	/**
	 * 展现所有的校友
	 * */
	public function index(){
		$school_id=$this->showschoolID();
		$alumni_id=I('post.alumni_id');
		if($school_id){	
			$alumni=M('SchoolAlumni');
			$map['school_id']=$school_id;
			if($alumni_id){
				$map['alumni_id']=$alumni_id;	//编辑时查询特定一条数据
			}
			$alumini_data=$alumni->where($map)->select();
			$this->assign("alumini_data",$alumini_data);
			$this->assign("school_id",$school_id);
			$this->display();
		}
	}
	
	
	/**
	 * 增加知名校友
	 *  
	 * */
	public function addAlumni(){
		$pic_path = $this->upload();
		$school_id=$this->showschoolID();
		if($school_id){
			$alumni=M('SchoolAlumni');
			$data=array(
				'school_id'			=>	I('post.school_id'),
				'alumni_name'		=>	I('post.alumni_name'),
				'alumni_content'	=>	I('post.alumni_content'),
				'alumni_pic'		=>	$pic_path
			);
			
			$alumni_id=$alumni->add($data);
			if(!$alumni_id){
				$this->error('新增失败!');
			}
		}
		$this->redirect('Teachers/Alumni/index',$params, 0, '页面跳转中...');
	}
	
	
	/**
	 * 更新校友信息
	 * */
	public function updateAlumni(){
		$pic_path = $this->upload();
		$alumni_id=I('post.alumni_id');
		$school_id=$this->showschoolID();
		if($alumni_id && $school_id){
			$map['alumni_id']=$alumni_id;
			$map['school_id']=$school_id;
			$alumni=M('SchoolAlumni');
			$data=array(
				'alumni_name'	=> I('post.alumni_id'),
				'alumni_content'=> I('post.alumni_content'),
				'alumni_name'	=> I('post.alumni_name'),
			);
			if(!empty($pic_path)){
				$data['alumni_pic']=$pic_path;
			}
		 	$flag=$alumni->where($map)->save($data);

		}
		$this->redirect('Teachers/Alumni/index',$params, 0, '页面跳转中...');
	}
	
	
	/**
	 *删除知名校友 
	 * 
	 * */
	public function deleteAlumni(){
		$alumni_id=I('post.alumni_id');
		$school_id=$this->showschoolID();
		if($alumni_id && $school_id){
			$map['alumni_id']=$alumni_id;
			$map['school_id']=$school_id;
			$alumni=M('SchoolAlumni');
			$data=$alumni->where($map)->find();
			unlink('./Public/'.$data['alumni_pic']);
		 	$flag=$alumni->where($map)->delete();
			if(!$flag){
				$this->error('删除失败！');
			}
		}
	}
	

	public function upload(){
	
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Public/'; // 设置附件上传根目录
	    $upload->savePath  =     'Teachers/images/alumni/'; // 设置附件上传（子）目录
	    $upload->saveName  = 	 time().'_'.mt_rand();
	    // 上传文件 
	    if(isset($_FILES['alumni_pic'])){
		    $info = $upload->uploadOne($_FILES['alumni_pic']);
		    if(!$info) {// 上传错误提示错误信息
//		        $this->error($upload->getError());
				return '';
		    }else{// 上传成功
		    	$image = new \Think\Image(); 
				$image->open('./Public/'.$info['savepath'].$info['savename']);
				// 按照原图的比例生成一个最大为60*60的缩略图并保存为thumb.jpg
				$image->thumb(60, 60)->save('./Public/'.$info['savepath'].'thumb_'.$info['savename']);
		       return $info['savepath'].'thumb_'.$info['savename'];
		    }
	    }else{
	    	return '';
	    }    	
	    
	}

	

}